/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.app.utils.security;

import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Santiago
 */
public class LoginPhaseListener implements PhaseListener {

  @Override
  public void afterPhase(PhaseEvent event) {
    FacesContext facesContext = event.getFacesContext();
    String currentPage = facesContext.getViewRoot().getViewId();
    
    boolean isLoginPage = (currentPage.lastIndexOf("login.xhtml") > -1);
    HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);

    if (session == null) {
      NavigationHandler nh = facesContext.getApplication().getNavigationHandler();
      nh.handleNavigation(facesContext, null, "login");
    } else {
      Object currentUser = session.getAttribute("username");

      if (!isLoginPage && (currentUser == null || currentUser == "")) {
        NavigationHandler nh = facesContext.getApplication().getNavigationHandler();
        nh.handleNavigation(facesContext, null, "login");
      }
    }
  }

  @Override
  public void beforePhase(PhaseEvent event) {
  }

  @Override
  public PhaseId getPhaseId() {
    return PhaseId.RESTORE_VIEW;
  }
}
